package com.orange.cetback.controller.admin;

import com.orange.cetback.common.utils.AliOssUtil;
import com.orange.cetback.common.vo.PageResult;
import com.orange.cetback.common.vo.Result;
import com.orange.cetback.pojo.dto.PaperDTO;
import com.orange.cetback.pojo.entity.ExamPapers;
import com.orange.cetback.service.ExamPapersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin-api/paper")
@Tag(name = "试卷接口")
public class PaperController {
    @Autowired
    private ExamPapersService examPapersService;
    @Autowired
    private AliOssUtil aliOssUtil;

    @Operation(summary = "分页查询试卷信息")
    @PostMapping("/page")
    public Result<PageResult<ExamPapers>> page(@RequestBody PaperDTO paperDTO){
        log.info("分页查询试卷信息：{}",paperDTO);
        PageResult<ExamPapers> pageResult = examPapersService.pageQuery(paperDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "新增试卷信息")
    @PostMapping("/save")
    public Result<String> add(@RequestBody ExamPapers examPapers){
        log.info("新增试卷信息：{}",examPapers);
        examPapersService.save(examPapers);
        return Result.success("新增试卷成功");
    }

    @Operation(summary = "修改试卷信息")
    @PutMapping("/update")
    public Result<String> update(@RequestBody ExamPapers examPapers){
        log.info("修改试卷信息：{}",examPapers);
        examPapersService.updateById(examPapers);
        return Result.success("修改试卷成功");
    }


    //上传文件接口
    @Operation(summary = "上传文件接口")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{}", file.getOriginalFilename());
        try {
            // 生成唯一文件名（防止覆盖）
            String originalFilename = file.getOriginalFilename();
            String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            // 调用OSS工具上传
            String url = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(url);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage());
        }
        return Result.fail("文件上传失败");
    }

    @Operation(summary = "上传试卷题目信息")
    @PostMapping("/upload/{paperId}")
    public Result<String> uploadPaperQuestion(@PathVariable Integer paperId, @RequestParam("file") MultipartFile file){
        log.info("上传试卷题目信息：{}", paperId);
        examPapersService.uploadPaperQuestion(paperId, file);
        return Result.success("上传试卷题目信息成功");
    }

    @Operation(summary = "获取全部试卷信息")
    @GetMapping("/all")
    public Result<List<ExamPapers>> getAll(){
        log.info("获取全部试卷信息");
        List<ExamPapers> list = examPapersService.list();
        return Result.success(list);
    }
}
